package com.taxshare.purchaser.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.constant.ResultConstant;
import com.taxshare.purchaser.entity.InvoiceBlackRulesEntity;
import com.taxshare.purchaser.entity.InvoiceBuyerRulesEntity;
import com.taxshare.purchaser.mapper.InvoiceBlackRulesMapper;
import com.taxshare.purchaser.view.dto.InvoiceBlackRulesDTO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: InvoiceBlackRulesService
 * @Description: 黑名单审核规则操作
 * @author: hjh
 * @date: 2021/4/28 14:46
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
public class InvoiceBlackRulesService extends ServiceImpl<InvoiceBlackRulesMapper, InvoiceBlackRulesEntity> {
    /**
     * 更新是否开启状态
     * @param bean
     * @return
     */
    public Response updateBlackIsOpen(InvoiceBlackRulesDTO bean) {
        if(StrUtil.hasBlank(bean.getUuid())){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        if(!CodeConstant.RULES_NOT_OPEN.equals(bean.getBlackIsOpen())&&(!CodeConstant.RULES_OPEN.equals(bean.getBlackIsOpen()))){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        InvoiceBlackRulesEntity flg = baseMapper.selectOne(new QueryWrapper<InvoiceBlackRulesEntity>().eq("uuid",bean.getUuid()));
        if(flg==null){
            InvoiceBlackRulesEntity entity = new InvoiceBlackRulesEntity();
            entity.setBlackRuleId(UUIDUitl.UUID32(false));
            entity.setUuid(bean.getUuid());
            entity.setBlackIsOpen(bean.getBlackIsOpen());
            int result = baseMapper.insert(entity);
            if(result!=1){
                return Response.error(ResultConstant.RET_D0001_CODE,ResultConstant.RET_D0001_MSG);
            }
            return Response.success();
        }
        UpdateWrapper<InvoiceBlackRulesEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uuid",bean.getUuid())
                     .set("black_is_open",bean.getBlackIsOpen());
        InvoiceBlackRulesEntity entity = new InvoiceBlackRulesEntity();
        int result = baseMapper.update(entity,updateWrapper);
        if(result!=1){
            return Response.error(ResultConstant.RET_D0002_CODE,ResultConstant.RET_D0002_MSG);
        }
        return Response.success();
    }
}
